package www.way2inbox.settings;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.way2sms.way2inbox.R;

public class SettingsGeneralActivity extends PreferenceActivity {
    SharedPreferences preferences = null;
    ListPreference p;
    Dialog d;
     String name;
    @Override
	protected void onCreate(Bundle savedInstanceState) {
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_settings);
		((TextView) findViewById(R.id.title)).setText("General Settings");
		addPreferencesFromResource(R.xml.settings_generalsms);

        p = (ListPreference)findPreference(
                ApplicationConstants.SettingsPreferenceKeys.PASSWORD);

        preferences = PreferenceManager
                .getDefaultSharedPreferences(getBaseContext());
        sinaturePref();
        delaytoSendPref();

          name = preferences.getString(
                ApplicationConstants.SettingsPreferenceKeys.PASSWORD,
                "disabled");

        p.setSummary(name);
        p.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
            public boolean onPreferenceChange(final Preference preference,
                                              Object newValue) {
                // Never hits here!
                name = preferences.getString(
                        ApplicationConstants.SettingsPreferenceKeys.PASSWORD,
                        "disabled");
                String v = (String) newValue;
                if(name.equals(v)){
         AlertDialog.Builder alert=new AlertDialog.Builder(SettingsGeneralActivity.this);
         alert.setTitle("PASSWORD");
                    alert.setMessage("It's Already "+v);
                    alert.setPositiveButton("ok",new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {

                        dialog.cancel();

                        }
                    });
                    alert.show();
                }else{
               // preference.setSummary(v);
                final String passwordvalue=preferences.getString("passwordvalue",null);
                System.out.println("this is on preference changed   " + passwordvalue);
                if(v.equals("enabled")){

                    if(passwordvalue!=null){
                         AlertDialog.Builder alert=new AlertDialog.Builder(SettingsGeneralActivity.this);
                         alert.setTitle("PASSWORD");
                        alert.setPositiveButton("Continue",new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                preference.setSummary("enabled");
                                p.setValue("enabled");

                            }
                        });
                        alert.setNeutralButton("Reset",new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                dialogInterface.cancel();
                                d=new Dialog(SettingsGeneralActivity.this);
                                d.setTitle("password");
                                d.setContentView(R.layout.dialog);
                                final EditText oldpassword=(EditText)d.findViewById(R.id.old_password);
                                //oldpassword.setVisibility(View.GONE);
                                final EditText newpassword=(EditText)d.findViewById(R.id.new_password);
                                final EditText re_password=(EditText)d.findViewById(R.id.Re_password);
                                Button ok=(Button)d.findViewById(R.id.ok);
                                Button cancel=(Button)d.findViewById(R.id.cancel);
                                ok.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View view) {
                                        String oldpassword1=oldpassword.getText().toString();
                                        String password=newpassword.getText().toString();
                                        String re_password1=re_password.getText().toString();
                                        if(password.equals(re_password1) && oldpassword1.equals(passwordvalue)){
                                            insertpassword(password);
                                            preference.setSummary("enabled");
                                            Toast.makeText(SettingsGeneralActivity.this,"password reseted succesfully",Toast.LENGTH_SHORT).show();
                                            d.cancel();
                                        }else{
                                            Toast.makeText(SettingsGeneralActivity.this,"password mismatch",Toast.LENGTH_SHORT).show();

                                        }

                                    }
                                });
                                cancel.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View view) {
                                        if(passwordvalue.equals(null)){
                                            preference.setSummary("disabled");
                                            changePreference("disabled");
                                            p.setValue("disabled");
                                        }else{
                                            preference.setSummary(name);
                                            p.setValue(name);
                                        }
                                        d.cancel();
                                    }
                                });
                                d.show();

                            }
                        });
                        alert.show();
                    }if(passwordvalue==null){
                        d=new Dialog(SettingsGeneralActivity.this);
                        d.setTitle("password");
                        d.setContentView(R.layout.dialog);
                        final EditText oldpassword=(EditText)d.findViewById(R.id.old_password);
                        oldpassword.setVisibility(View.GONE);
                        final EditText newpassword=(EditText)d.findViewById(R.id.new_password);
                        final EditText re_password=(EditText)d.findViewById(R.id.Re_password);
                        Button ok=(Button)d.findViewById(R.id.ok);
                        Button cancel=(Button)d.findViewById(R.id.cancel);
                        ok.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {

                                    String password=newpassword.getText().toString();
                                    String re_password1=re_password.getText().toString();
                                    if(password.equals(re_password1)){
                                        insertpassword(password);
                                        preference.setSummary("enabled");
                                        d.cancel();
                                    }else{
                                        Toast.makeText(SettingsGeneralActivity.this,"password mismatch",Toast.LENGTH_SHORT).show();

                                    }

                            }
                        });
                        cancel.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                if(passwordvalue.equals(null)){
                                    preference.setSummary("disabled");
                                    changePreference("disabled");
                                    p.setValue("disabled");
                                }
                                d.cancel();
                            }
                        });
                        d.show();
                    }
                }
                if(v.equals("disabled")){
        if(passwordvalue.equals(null)){
               Toast.makeText(SettingsGeneralActivity.this,"disabled",Toast.LENGTH_SHORT).show();
                  }else{

            d=new Dialog(SettingsGeneralActivity.this);
            d.setTitle("password");
            d.setContentView(R.layout.dialog);
            final EditText oldpassword=(EditText)d.findViewById(R.id.old_password);
            oldpassword.setVisibility(View.GONE);
            final EditText newpassword=(EditText)d.findViewById(R.id.new_password);
            final EditText re_password=(EditText)d.findViewById(R.id.Re_password);
            re_password.setVisibility(View.GONE);
            Button ok=(Button)d.findViewById(R.id.ok);
            Button cancel=(Button)d.findViewById(R.id.cancel);
            ok.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    String password=newpassword.getText().toString();
                    if(password.equals(passwordvalue)){
                       // insertpassword(password);
                        preference.setSummary("disabled");
                        Toast.makeText(SettingsGeneralActivity.this,"password is disabled succesfully",Toast.LENGTH_SHORT).show();
                        d.cancel();
                    }else{
                        Toast.makeText(SettingsGeneralActivity.this,"password mismatch",Toast.LENGTH_SHORT).show();

                    }

                }
            });
            cancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (passwordvalue.equals(null)) {
                        preference.setSummary("disabled");
                        p.setValue("disabled");
                        changePreference("disabled");
                    } else {
                        p.setValue(name);
                        preference.setSummary(name);

                    }
                    d.cancel();
                }
            });
            d.show();
        }
                }}
                return true;
            }

        });
	}
    public void insertpassword(String password){
        SharedPreferences.Editor ed=preferences.edit();
        ed.putString("passwordvalue",password);
        ed.commit();
        Toast.makeText(SettingsGeneralActivity.this,"password set succesfully",Toast.LENGTH_SHORT).show();
    }
    public void changePreference(String status){
        SharedPreferences.Editor ed=preferences.edit();
        ed.putString(ApplicationConstants.SettingsPreferenceKeys.PASSWORD,status);
        ed.commit();
    }

    private void sinaturePref() {
        getPreferenceScreen()
                .findPreference(
                        ApplicationConstants.SettingsPreferenceKeys.GEN_SIGNATURE)
                .setSummary(
                        preferences.getString(
                                ApplicationConstants.SettingsPreferenceKeys.GEN_SIGNATURE,
                                ""));
        getPreferenceScreen().findPreference(
                ApplicationConstants.SettingsPreferenceKeys.GEN_SIGNATURE)
                .setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {

                            @Override
                            public boolean onPreferenceChange(
                                    Preference preference, Object newValue) {
                                preference.setSummary((String) newValue);
                                return true;
                            }
                        });

    }
    private void delaytoSendPref() {
        getPreferenceScreen()
                .findPreference(
                        ApplicationConstants.SettingsPreferenceKeys.DELAY_TO_SEND)
                .setSummary(
                        preferences.getString(
                                ApplicationConstants.SettingsPreferenceKeys.DELAY_TO_SEND,
                                "2 sec"));

        getPreferenceScreen().findPreference(
                ApplicationConstants.SettingsPreferenceKeys.DELAY_TO_SEND)
                .setOnPreferenceChangeListener(
                        new Preference.OnPreferenceChangeListener() {

                            @Override
                            public boolean onPreferenceChange(
                                    Preference preference, Object newValue) {
                                String s=(String) newValue;
                                if(s.equals("disabled")){
                                    preference.setSummary("disabled");
                                }else{
                                preference.setSummary((String) newValue
                                        + " sec");}
                                return true;
                            }
                        });

    }
}
